﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyBlogDataPump : ICompanyBlogAccess
    {
        public List<DefaultDataPack> GetBlogList(string copName, int page, int size)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_blog
                              where c.c_vc_cpname == copName && !c.c_i_isDelete
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_nvc_title,
                                  c.c_nvc_author,
                                  c.c_i_hit,
                                  c.c_dt_pbdate,
                                  c.c_i_id,
                                  c.c_nvc_content,
                                  c.c_nvc_url
                              }).Skip(page * size).Take(size).ToList();
                if (result != null)
                {
                    return result.Select(c => new DefaultDataPack() {
                        TITLE = c.c_nvc_title,
                        AUTHOR = c.c_nvc_author,
                        HIT = c.c_i_hit,
                        DATE = c.c_dt_pbdate.ToString("yyyy-MM-dd"),
                        ID = c.c_i_id,
                        CONTENT = c.c_nvc_content,
                        URL = c.c_nvc_url
                    }).ToList();
                }
                else
                {
                    list.Add(new DefaultDataPack());
                }
                return list;
            }
        }

        public int GetBlogItemCount(string copName)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = ctx.tb_blog
                    .Where(c => c.c_vc_cpname == copName && !c.c_i_isDelete)
                    .Count();
                return result;
            }
        }

        public List<DefaultDataPack> GetBlogArticle(string copName, int articleId)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_blog
                              where c.c_vc_cpname == copName && c.c_i_id == articleId
                              select new
                              {
                                  c.c_dt_pbdate,
                                  c.c_i_hit,
                                  c.c_nvc_content,
                                  c.c_nvc_title,
                                  c.c_nvc_author,
                                  c.c_nvc_url
                              }).FirstOrDefault();
                if (result != null)
                {
                    list.Add(new DefaultDataPack()
                    {
                        DATE = result.c_dt_pbdate.ToLongDateString(),
                        HIT = result.c_i_hit,
                        CONTENT = result.c_nvc_content,
                        TITLE = result.c_nvc_title,
                        AUTHOR = result.c_nvc_author,
                        URL = result.c_nvc_url
                    });
                }
                return list;
            }
        }
        
        public int DeleteBlogArticle(string copName, int id)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
               var item = ctx.tb_blog.FirstOrDefault(c => c.c_vc_cpname == copName && c.c_i_id == id);
               item.c_i_isDelete = true;
               return ctx.SaveChanges();
            }
        }


        public bool UpdateBlogArticle(string copname, BlogDataPack blogDataPack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = ctx.tb_blog.FirstOrDefault(c => c.c_vc_cpname == copname && c.c_i_id == blogDataPack.Bid);
                if (item != null)
                {
                    item.c_dt_pbdate =DateTime.Parse(blogDataPack.Date);
                    item.c_nvc_author = blogDataPack.Author;
                    item.c_i_hit = blogDataPack.Hit;
                    item.c_nvc_content = blogDataPack.Content;
                    if (!String.IsNullOrEmpty(blogDataPack.MainPhoto))
                    {
                        item.c_nvc_url = blogDataPack.MainPhoto;
                    }
                    item.c_nvc_title = blogDataPack.Title;
                    return ctx.SaveChanges() == 1;
                }
                return false;
            }
        }


        public bool AddBlogArticle(string copname, BlogDataPack blogDataPack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.tb_blog.AddObject(
                    new ORM.CompanyData.tb_blog()
                    {
                        c_vc_cpname = copname,
                        c_i_isDelete = false,
                        c_dt_pbdate = DateTime.Parse(blogDataPack.Date),
                        c_nvc_author = blogDataPack.Author,
                        c_i_hit = blogDataPack.Hit,
                        c_nvc_content = blogDataPack.Content,
                        c_nvc_url = blogDataPack.MainPhoto,
                        c_nvc_title = blogDataPack.Title
                    });
                return ctx.SaveChanges() == 1;
            }
        }
    }
}
